package com.hung.trade.pay.api;

import com.hung.trade.common.api.IPayApi;
import com.hung.trade.common.constants.TradeEnums;
import com.hung.trade.common.protocol.pay.CallbackPaymentReq;
import com.hung.trade.common.protocol.pay.CreatePaymentReq;
import com.hung.trade.common.protocol.pay.CallbackPaymentResp;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyReq;
import com.hung.trade.common.protocol.pay.ChangeUserMoneyResp;
import com.hung.trade.common.protocol.pay.CreatePaymentResp;
import com.hung.trade.pay.service.IPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author hungki
 * @createTime 2018-10-09 下午 4:27
 */
@Controller
public class PayApiImpl implements IPayApi {
    @Autowired
    private IPayService iPayService;
    @Override
    @RequestMapping(value = "/changeUserMoney",method = RequestMethod.POST)
    @ResponseBody
    public ChangeUserMoneyResp changeUserMoney(@RequestBody ChangeUserMoneyReq changeUserMoneyReq) {
        ChangeUserMoneyResp resp = new ChangeUserMoneyResp();
        try{
            resp = iPayService.changeUserMoney(changeUserMoneyReq);
        }catch (Exception e){
            e.printStackTrace();
            resp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
            resp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
        }
        return resp;
    }

    @Override
    @RequestMapping(value = "/createPayment",method = RequestMethod.POST)
    @ResponseBody
    public CreatePaymentResp createPayment(@RequestBody CreatePaymentReq createPaymentReq) {
        CreatePaymentResp resp = new CreatePaymentResp();
        try{
            resp = iPayService.createPayment(createPaymentReq);
        }catch (Exception e){
            e.printStackTrace();
            resp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
            resp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
        }
        return resp;
    }

    @Override
    @RequestMapping(value = "/callbackPayment",method = RequestMethod.POST)
    @ResponseBody
    public CallbackPaymentResp callbackPayment(@RequestBody CallbackPaymentReq callbackPaymentReq) {
        CallbackPaymentResp resp = new CallbackPaymentResp();

        try{
            resp = iPayService.callbackPayment(callbackPaymentReq);
        }catch (Exception e){
            e.printStackTrace();
            resp.setRetInfo(TradeEnums.RetEnum.FAIL.getDesc());
            resp.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
        }

        return resp;
    }
}
